﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;
namespace TestDxfDocument
{
    class class_ISYC_304_04
    {
        public static void ISYC_304_F39(Dictionary<string, Object> data) //心轴式整体周边和端面铣刀
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-------------------------------------------------------------------------------------------------
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);
            double scale = 1;

            //赋值参数--------------------------------------------------------------------------------------------------
            /*double DC = 42.79 * scale;
            double DHUB = 11.97 * scale;
            double CDX = 14.65 * scale;
            double DCONMS = 5.29 * scale;
            double LTA = 4.99 * scale;
            double CW = 2.14 * scale;
            double OAL = 7.1 * scale;
            double KAPR = 90;*/

            double DC = Convert.ToDouble(data["dc"]);
            double DHUB = Convert.ToDouble(data["dhub"]);
            double CDX = Convert.ToDouble(data["cdx"]);
            double DCONMS = Convert.ToDouble(data["dconms"]);
            double LTA = Convert.ToDouble(data["lta"]);
            double CW = Convert.ToDouble(data["cw"]);
            double OAL = Convert.ToDouble(data["oal"]);
            double KAPR = Convert.ToDouble(data["kapr"]);

            //点---------------------------------------------------------------------------------------------
            //第一部分
            //中心线
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = fun.Polar(v1, 0 * pi / 180, OAL);

            //轮廓
            Vector2 v4 = new Vector2(v1.X + (0.5 / 7.1) * OAL, v1.Y + DHUB / 2.0);
            Vector2 v3 = fun.Inters(v1, fun.Polar(v1, 90 * pi / 180, DC), v4, fun.Polar(v4, -135 * pi / 180, OAL));
            Vector2 v5 = fun.Polar(v1, 0 * pi / 180, OAL / 2.0 - CW / 2.0);
            Vector2 v6 = fun.Inters(v4, fun.Polar(v4, 0 * pi / 180, OAL), v5, fun.Polar(v5, 90 * pi / 180, DC));
            Vector2 v7 = fun.Polar(v6, 90 * pi / 180, (2.14 / 14.65) * CDX);
            Vector2 v8 = fun.Polar(v7, 180 * pi / 180, (0.37 / 2.14) * CW);
            Vector2 v9 = fun.Polar(v8, 90 * pi / 180, (2.89 / 14.65) * CDX);
            Vector2 v10 = fun.Polar(v7, 90 * pi / 180, (2.89 / 14.65) * CDX);
            Vector2 v11 = fun.Polar(v10, 90 * pi / 180, (9.59 / 14.65) * CDX);
            Vector2 v12 = fun.Polar(v11, 0 * pi / 180, CW);
            Vector2 v13 = fun.Polar(v10, 0 * pi / 180, CW);
            Vector2 v14 = fun.Polar(v13, 0 * pi / 180, (0.37 / 2.14) * CW);
            Vector2 v15 = fun.Polar(v14, -90 * pi / 180, (2.89 / 14.65) * CDX);
            Vector2 v16 = fun.Polar(v15, 180 * pi / 180, (0.37 / 2.14) * CW);
            Vector2 v17 = fun.Polar(v16, -90 * pi / 180, (2.14 / 14.65) * CDX);
            Vector2 v18 = fun.Polar(v17, 0 * pi / 180, fun.CalDistance(v4, v6));
            Vector2 v19 = new Vector2(v18.X + (0.5 / 7.1) * OAL, v3.Y);
            Vector2 v20 = fun.mirrorpoint(v19, v1, v2);
            Vector2 v21 = fun.mirrorpoint(v18, v1, v2);
            Vector2 v22 = fun.mirrorpoint(v17, v1, v2);
            Vector2 v23 = fun.Polar(v22, -90 * pi / 180, (13.08 / 42.79) * DC);
            Vector2 v24 = fun.Polar(v23, 180 * pi / 180, fun.CalDistance(v11, v12));
            Vector2 v25 = fun.mirrorpoint(v6, v1, v2);
            Vector2 v26 = fun.mirrorpoint(v4, v1, v2);
            Vector2 v27 = fun.mirrorpoint(v3, v1, v2);
            Vector2 v28 = fun.Polar(v1, -90 * pi / 180, DCONMS / 2.0);
            Vector2 v29 = fun.Inters(v28, fun.Polar(v28, 0 * pi / 180, OAL * 2), v19, v20);
            Vector2 v30 = fun.Polar(v10, 90 * pi / 180, (4.79 / 14.65) * CDX);
            Vector2 v31 = fun.Polar(v30, 180 * pi / 180, fun.CalDistance(v9, v10));
            Vector2 v32 = fun.Polar(v31, 90 * pi / 180, (5.8 / 14.65) * CDX);
            Vector2 v33 = fun.Polar(v32, 0 * pi / 180, OAL - (OAL - LTA) * 2);
            Vector2 v34 = fun.Polar(v33, -90 * pi / 180, fun.CalDistance(v31, v32));
            Vector2 v35 = fun.Inters(v34, fun.Polar(v34, 180 * pi / 180, OAL), v12, v13);
            Vector2 v36 = fun.Polar(v22, -90 * pi / 180, (9.51 / 42.79) * DC);
            Vector2 v37 = fun.Polar(v36, 0 * pi / 180, fun.CalDistance(v30, v31));
            Vector2 v38 = fun.Polar(v37, -90 * pi / 180, fun.CalDistance(v31, v32));
            Vector2 v39 = fun.Polar(v38, 180 * pi / 180, fun.CalDistance(v32, v33));
            Vector2 v40 = fun.Polar(v39, 90 * pi / 180, fun.CalDistance(v37, v38));
            Vector2 v41 = fun.Inters(v40, fun.Polar(v40, 0 * pi / 180, OAL), v25, v39);

            //作图部分-----------------------------------------------------------------------
            //第一部分
            //中心线
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.Center;
            line1.LinetypeScale = 5;
            dxf.AddEntity(line1);

            //轮廓
            LwPolyline polyline1 = new LwPolyline();
            polyline1.Vertexes.Add(new LwPolylineVertex(v1));
            polyline1.Vertexes.Add(new LwPolylineVertex(v3));
            polyline1.Vertexes.Add(new LwPolylineVertex(v4));//1
            polyline1.Vertexes.Add(new LwPolylineVertex(v6));
            polyline1.Vertexes.Add(new LwPolylineVertex(v7));
            polyline1.Vertexes.Add(new LwPolylineVertex(v8));
            polyline1.Vertexes.Add(new LwPolylineVertex(v9));
            polyline1.Vertexes.Add(new LwPolylineVertex(v10));
            polyline1.Vertexes.Add(new LwPolylineVertex(v11));
            polyline1.Vertexes.Add(new LwPolylineVertex(v12));//8
            polyline1.Vertexes.Add(new LwPolylineVertex(v13));
            polyline1.Vertexes.Add(new LwPolylineVertex(v14));
            polyline1.Vertexes.Add(new LwPolylineVertex(v15));
            polyline1.Vertexes.Add(new LwPolylineVertex(v16));
            polyline1.Vertexes.Add(new LwPolylineVertex(v17));
            polyline1.Vertexes.Add(new LwPolylineVertex(v18));
            polyline1.Vertexes.Add(new LwPolylineVertex(v19));//15
            polyline1.Vertexes.Add(new LwPolylineVertex(v20));
            polyline1.Vertexes.Add(new LwPolylineVertex(v21));//17
            polyline1.Vertexes.Add(new LwPolylineVertex(v22));
            polyline1.Vertexes.Add(new LwPolylineVertex(v23));
            polyline1.Vertexes.Add(new LwPolylineVertex(v24));//20
            polyline1.Vertexes.Add(new LwPolylineVertex(v25));
            polyline1.Vertexes.Add(new LwPolylineVertex(v26));
            polyline1.Vertexes.Add(new LwPolylineVertex(v27));//23
            polyline1.Vertexes.Add(new LwPolylineVertex(v1));
            polyline1.Vertexes[1].Bulge = Math.Tan(-90 * pi / 180 / 4);
            polyline1.Vertexes[8].Bulge = Math.Tan(-108 * pi / 180 / 4);
            polyline1.Vertexes[15].Bulge = Math.Tan(-90 * pi / 180 / 4);
            polyline1.Vertexes[17].Bulge = Math.Tan(-90 * pi / 180 / 4);
            polyline1.Vertexes[20].Bulge = Math.Tan(-108 * pi / 180 / 4);
            polyline1.Vertexes[23].Bulge = Math.Tan(-90 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline1, 0.3, contour);

            LwPolyline polyline2 = new LwPolyline();
            polyline2.Vertexes.Add(new LwPolylineVertex(v30));
            polyline2.Vertexes.Add(new LwPolylineVertex(v31));
            polyline2.Vertexes.Add(new LwPolylineVertex(v32));
            polyline2.Vertexes.Add(new LwPolylineVertex(v33));
            polyline2.Vertexes.Add(new LwPolylineVertex(v34));
            polyline2.Vertexes.Add(new LwPolylineVertex(v35));
            fun.PolyFill(dxf, polyline2, 0.3, contour);

            LwPolyline polyline3 = new LwPolyline();
            polyline3.Vertexes.Add(new LwPolylineVertex(v10));
            polyline3.Vertexes.Add(new LwPolylineVertex(v13));
            fun.PolyFill(dxf, polyline3, 0.3, contour);

            LwPolyline polyline4 = new LwPolyline();
            polyline4.Vertexes.Add(new LwPolylineVertex(v7));
            polyline4.Vertexes.Add(new LwPolylineVertex(v16));
            fun.PolyFill(dxf, polyline4, 0.3, contour);

            LwPolyline polyline5 = new LwPolyline();
            polyline5.Vertexes.Add(new LwPolylineVertex(v6));
            polyline5.Vertexes.Add(new LwPolylineVertex(v5));
            fun.PolyFill(dxf, polyline5, 0.3, contour);

            LwPolyline polyline6 = new LwPolyline();
            polyline6.Vertexes.Add(new LwPolylineVertex(v17));
            polyline6.Vertexes.Add(new LwPolylineVertex(fun.Inters(v17, fun.Polar(v17, -90 * pi / 180, DC), v1, v2)));
            fun.PolyFill(dxf, polyline6, 0.3, contour);

            LwPolyline polyline7 = new LwPolyline();
            polyline7.Vertexes.Add(new LwPolylineVertex(v36));
            polyline7.Vertexes.Add(new LwPolylineVertex(v37));
            polyline7.Vertexes.Add(new LwPolylineVertex(v38));
            polyline7.Vertexes.Add(new LwPolylineVertex(v39));
            polyline7.Vertexes.Add(new LwPolylineVertex(v40));
            polyline7.Vertexes.Add(new LwPolylineVertex(v41));
            fun.PolyFill(dxf, polyline7, 0.3, contour);

            LwPolyline polyline8 = new LwPolyline();
            polyline8.Vertexes.Add(new LwPolylineVertex(v28));
            polyline8.Vertexes.Add(new LwPolylineVertex(v29));
            fun.PolyFill(dxf, polyline8, 0.3, contour);

            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v28));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v29));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v20));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v21));//2
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v22));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v23));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v24));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v25));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v26));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v27));//8
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v28));
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);

            //标注---------------------------------------------------------------------------
            double height = 2 * scale;
            double arrowsize = 1 * scale;
            double exline = 0.5 * scale;

            //坐标系
            /*fun.Axis(dxf, v1, 180, 270, "", "", "PCS", 0.3 * scale, height, 0);
            fun.Axis(dxf, fun.Polar(v39, -120 * pi / 180, 19.36 * scale), 180, 270, "Z", "Y", "X", 0.3 * scale, height, 0);
            */
            //直线标注
            //DCONMS
            fun.dimrot1(dxf, new Vector2(v1.X, v1.Y + DCONMS / 2.0), v28, 90, OAL, 0.1, height, arrowsize, exline, "DCONMS", "", "");

            //DHUB
            fun.dimrot1(dxf, v4, v26, 90, OAL * 2, 0.1, height, arrowsize, exline, "DHUB", "", "");

            //CDX
            fun.dimrot1(dxf, new Vector2(v6.X, v6.Y + fun.CalDistance(v6, v7) / 2.0), v32, 90, OAL * 2 + Math.Abs(v6.X - v4.X) - fun.CalDistance(v7, v8) / 2.0, 0.1, height, arrowsize, exline, "CDX", "", "");

            //DC
            fun.dimrot1(dxf, v33, v38, -90, OAL * 2, 0.1, height, arrowsize, exline, "DC", "", "");

            //OAL
            fun.dimrot1(dxf, v3, v19, 0, CDX * 1.5, 0.1, height, arrowsize, exline, "OAL", "", "");

            //CW
            fun.dimrot1(dxf, v24, v23, 180, CDX * 0.6, 0.1, height, arrowsize, exline, "CW", "", "");

            //LTA
            //fun.dimrot1(dxf, v26, v38, 180, CDX * 1.3, 0.1, height, arrowsize, exline, "LTA,LF", "", "");

            //角度标注
            //KAPR
            fun.jdbz2(dxf, new Line(v33, fun.Polar(v33, 90 * pi / 180, 0.1)), new Line(v33, fun.Polar(v33, 180 * pi / 180, 0.1)), OAL * 0.5, height, arrowsize, exline, "KAPR", "", "");

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
